package com.sq.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.sq.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝支付 配置类
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119632006";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCY40ASIirrpeLh1mxbjJkwBoB7pylqd0CF+nyaCDioHR61ZNUZECLIu+nops4lSKyclCskYgoXgRYFkwE9m/3dhEjfBeK3R4bF6ImOmcgvSvBSDhCd7Usu5SXgstd9P6CgbomplqEtaMWvFdNozVOjAOGAdx1OUVMCNvJHJP3CduytqQuwcuUG3geZlcbmMSekzLr8+uw+0c3zxZa7XoCJFw/zufyvYPIOKrXBnxzIm+UnWGv9jv1k1BQ6oJSkRdX1QtPmybxOHN+vLvWK6Ex4/tqctInBkXJadzs3TrngNWg7/JldzCgWeqhipJnBE2SJ89nvvjDJhUey/3EMCattAgMBAAECggEAFZq/WhCj+grGBd3UEBfNKeExVMfpbVcOsiAUbXvN7cu3pDBsgI8sKyuKwUqZBOHBKpsxgYGtJWcjYoREf51EgYII/iMJx5Z+8AaNMa5/gFdZoG7GYFrA7g6JjLgZBWTxYNpd764OdqlAjh5KcpvZPsaIFyw94uxRC5YSJIWQqmMFHiyU8vzw5C6rE0qkooV56BnLiM4cH+Bt8BeC1sAuAPSMvzdibB5ydl60SX0cJC0ZtGENRGpmpFOmltIInm7x17iI8IcHBcpwh9jEkLL7g9fMPtswxCvJeJ7Jt5TuvCff6KgtivM726O+Rvm1uYospDImmraR2PDv1Wu8bE2/IQKBgQDpgBqbeIFKW1fD01aL2+Ht03PiZnR/W/g9cwO2y9fHCmv90r5sHTYzPFDC6P5VFDdNFmWen+bCGSNmRUIR54y62JIEz9sOoYVUzgc3acxZ2CA2eMoDhcXkjo/S0sPwV9CVmC2A6UEcx2gCjhSsuOXJL3NBObpK2mdnp+P/MZ6QqQKBgQCnnp+vHif0CSmuj0XzKhh7NGZDTN3qsFvZoaPNIZ1Qx7Z+2r0aG44pFW7z6z+SEdi5h0QxQQdEF/ZejilQVNp9o+ioI58NS7Dv9hmpPwDoPVG+TMmq4MCIyCo9Hxz0IL5+iSiuKkXqMkCeu+Cms6Y1F7IHAnFZIpfztxXBL9uLJQKBgB80JVodcnmzVzkuJuStz7Vuio/4zRyv966OjjKBbyaEOAZFgXkFNlcGemxxontQJkvA8HeQOtA5d8eU/c1SujXF9015UXbVEvJQThqTouFmxndBEQ4DdHykqIet7s1RjuhO29XcVf04kKe2wQCL/PkzxB01T7XtFeB+STOzN9NxAoGAEp6cPmerKICcELf5nTlf3s/DTFaNkDRm/5XlDDRftUl6DD1gJDZLZGTtHktTlHZHDNTpzk7a78V5qKdCD5pdavd6MLNER8XQsdc0oohNIX8uRWViQYkV2hviFUgPf07j2kqCKlanzYwngXp9SgdKvzsSm5S54pzlJD2CPSODz8ECgYBQDlIaM6VoAtGOKu44dZHQ/j6ZJ3jYfV6XYTV/zffDhLgVr4erzm+YP7FmuRk6iCrIS50bhPw0Ltwb/DGZglus5l+0iqL6uQrM7U8EXWVymgdT7DU5Ssh+fe8TmcFBrhfOQ+Zwuo/mhb5prpMTyy5bTHloIk1CNbgH1FF9rd7AMQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxnpAtLor81cPoIiFzCb6zuaHYpWdc+NylEOmOj7KEuOREtX+j6MQy77R5l9ejLn10cLoHcAz2aut47KUtSm8q72W7t4sv+f5X31W2nzs46r/3BapebOa8OxuU5s8xHsF4cRL685SdWHvw4ZbvuTxETgT9MNHgE7JQCBlnltyEquMJI37jjkOT2B2RqnBE3K2XGAYia7PP06S4UGHQaQWYpsQY0FUX5xg3yNetBT7bjYBh547jZb6TF5annaMjJfB7hsXnHTvyA5fOjVg9PXOdgxZm76+b5ZnsuiRQst7qi/cNMLH1q621tM2xYYXEl4Q7mRK7zcCgO04FPT4wTjXTwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://3pfc56az9f.51xd.pub/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //设置过期时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
